<?php
    chdir('../../');
    require('inc/header.php');
    
    if(isset($_SESSION['user']['uid'])) {
        $uid = (int)$_SESSION['user']['uid'];
        $user = new AnyUser($uid);
        if($user->getUid() > 0) {
            $db = Database::getInstance();
            $term = $_GET['term'];
            if(isset($_GET['uid'])) {
                $where = "AND mid not in (select mid from user_rights where uid=" . $_GET['uid'] . ")";
            } else if(isset($_GET['gid'])) {
                $where = "AND mid not in (select mid from group_rights where gid=" . $_GET['gid'] . ")";
            } else {
                $where = '';
            }
            $db->query("SELECT
                            mid, name
                        FROM
                            modules
                        WHERE
                            name like '$term%'" . $where);
            $return = array();
            foreach($db->getObjectArray() as $key => $obj) {
                $return[] = array("mid"=>$obj->mid, "label"=>$obj->name);
            }
            die(json_encode($return));
        }
    }
?>
